// 版本控制
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table,  Modal, Select,message } from 'antd'
import Ueditor from '../../components/Ueditor.js'
/* 引用接口文档路径 */
import {
  versionControl,
  versionInfo,
  versionDelete,
  versionEdit,
  versionAdd,
} from 'actions/admin'//获取接口方法
import {getbasetypefetch} from 'actions/common'
import '../../style/userControl.less'
import ModalAddVersionControl from './addVersionControlModal.js';

const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      contentModal:false,//列表内容详情弹窗
      modalList:"",//列表内容详情
      addModalFlg:false,//新增报告弹窗
      addModalType:"",
      selectTypeList:[
        {code:'',value:'全部',key:'0'},
        {code:'IOS',value:'IOS',key:'1'},
        {code:'android',value:'android',key:'2'},
      ],
      shsblist:[],
    }
    this.keyword={"appName":"","versionCode":"",};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
    this.typeId = "";//报告类型
  }
  componentDidMount() {
    this.selectList();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "appName":this.props.form.getFieldValue('appName'),
      "versionCode":this.props.form.getFieldValue('versionCode'),
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(versionControl(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  

  addReport(type,id){//新增报告
    //编辑的情况  带数据去组件
    if(type == 2){
      this.props.dispatch(versionInfo({"id":id},
      (respose) => {
          this.setState({
          addModalFlg: true,
          modalList:respose,
          addModalType:type,
        })
      }))
    }else{//新增的情况
      this.setState({
        addModalFlg: true,
        modalList:{},
        addModalType:type,
      })
    }
  }

  addModalCancel(){//关闭新增报告弹窗
    this.setState({
      addModalFlg: false,
    })
  }

  contentModalCancel(){//关闭查看弹窗
    this.setState({
      contentModal: false,
    },()=>{
      this.setState({
        modalList: "",
      })
    })
  }

  showModal(id){//点击查看  大咖报告详情
    this.props.dispatch(versionInfo({"id":id},
      (respose) => {
        this.setState({
          contentModal: true,
          modalList:respose,
        })
    }))
  }

  deleteList(id){//查看弹窗里面的删除按钮
    Modal.confirm({
      content:"确定要删除该条信息么？",
      onOk:()=>{
        this.props.dispatch(versionDelete(
          {"id":id},
          (respose) => {
            var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
            if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
              this.currentNumber = this.currentNumber - 1;
            }
            message.success(respose.respMsg);
            this.selectList(this.currentNumber);
        }))
      },
      onCancel:()=>{
      }
    })
    
  }

  columns() {//表格标题
    return [
      {
        title: '版本号',
        key: 'versionCode',
        dataIndex: 'versionCode',
        width: '10%',
      },
      {
        title: '下载地址',
        dataIndex: 'downloadUrl',
        key: 'downloadUrl',
        width: '10%',        
      },
      {
        title: '是否强制更新',
        dataIndex: 'forceUpdate',
        key: 'forceUpdate',
        width: '10%',
        render: (text) =><div>{text?'是':'否'}</div>
      },
      {
        title: '是否更新',
        dataIndex: 'update',
        key: 'update',
        width: '10%',
        render: (text) =><div>{text?'是':'否'}</div>        
      },
      {
        title: '编辑时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '10%',
      },
      {
        title: '版本说明',
        dataIndex: 'versionExplain',
        key: 'versionExplain',
        width: '10%',
      },
      {
        title: '设备名称',
        dataIndex: 'appName',
        key: 'appName',
        width: '10%',
      },
      {
        title: '联系电话',
        dataIndex: 'mobile',
        key: 'mobile',
        width: '10%',
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          {/* <Button type="primary" size="small" onClick={this.showModal.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>查看</Button>    */}
          <Button type="primary" size="small" onClick={this.addReport.bind(this,2,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>      
          <Button type="primary" size="small" onClick={this.deleteList.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>删除</Button>                
        </div>,        
      },
    ]
  }

  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const respData = this.state.list.respData ? this.state.list.respData : [];//刷新页面调用接口数据赋值
    const modalList = this.state.modalList.respData ? this.state.modalList.respData :[];
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="设备类型">      
              {
                getFieldDecorator('appName', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Select placeholder="请选择报告类型"    style={{ width: 150 }} >
                    {
                      this.state.selectTypeList.map((item,i)=> {
                        return(
                          <Select.Option value={item.code} key={i}>{item.value}</Select.Option>
                        )
                      })
                    }
                  </Select> 
                )
              }                      
            </FormItem>
            <FormItem label="版本号">
              {
                getFieldDecorator('versionCode', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
            <Button type="primary" onClick={this.addReport.bind(this,1)}>新增</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列 
            dataSource={respData.resultData}
            pagination={{ total: respData.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
            rowClassName={(record, index) => record.dataStatus == 2 ? 'tableRed' : ''} 
          />
        </Spin>
        {/* 新增报告弹窗 */}
        {
          this.state.addModalFlg ?
          <ModalAddVersionControl 
            type={this.state.addModalType}
            ModalFlg={this.state.addModalFlg} 
            ModalCancel={this.addModalCancel.bind(this)} 
            callback={this.selectList.bind(this)}
            content={this.state.modalList.respData ? this.state.modalList.respData : {}}
          /> : ""
        }
        
        {/* 信息展示弹窗-暂不做 */}
        {
          this.state.contentModal?
          <Modal
            width={"80%"}
            visible={this.state.contentModal}
            title="报告详情"
            onCancel={this.contentModalCancel.bind(this)}
            footer={''}
          >
            <ul className="joinBlackListModal">
              <li>暂无数据</li>            
            </ul>
          </Modal>:''
        }
        
      </div>
    )
  }
}
